<template>
    <div class="main-container">
        <div v-if="!dialogSettingVisible">
            <el-card class="box-card">
                <div slot="header" class="clearfix">
                    <span><i class="el-icon-condition"></i></span>
                </div>
                <div style="height: 40px;">
                    <el-form :inline="true" :model="formQuery">
                        <el-form-item label="角色名">
                            <el-input size="small" v-model="formQuery.name" placeholder="角色名"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="onSubmit(null,null)" size="small">
                                查询
                            </el-button>
                            <el-button type="info" icon="el-icon-refresh-left" @click="onReset" size="small">
                                重置
                            </el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-card>
            <el-card class="box-card table-list">
                <div slot="header" class="clearfix">
                    <span><i class="el-icon-table-list" style="font-weight: 600;margin-top: 8px;"></i></span>
                    <el-button class="el-icon-u-tj" style="float: right; padding: 3px 0;font-size: 20px" type="text"
                               @click="handleClick(null)"></el-button>
                </div>
                <div>
                    <el-table :data="tableData" height="calc(100vh - 400px)" border style="width: 100%"
                              empty-text="暂无数据"
                              :loading="loading">
                        <el-table-column type="index" width="50" label="序号"></el-table-column>
                        <el-table-column align="center" prop="name" label="角色名"></el-table-column>
                        <el-table-column align="center" prop="description" label="角色描述"></el-table-column>
                        <el-table-column align="center" prop="status" label="角色状态" width="100">
                            <template slot-scope="scope">
                                <span v-if="scope.row.status ===1" style="color: #92e268">启用</span>
                                <span v-else style="color: #ff2b4a">禁用</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" fixed="right" label="操作" width="120">
                            <template slot-scope="scope">
                                <el-button @click="handleClick(scope.row)" type="text"
                                           icon="el-icon-edit-outline"
                                           style="font-size: 18px">
                                </el-button>
                                <el-button @click="handleSettingClick(scope.row)" type="text"
                                           icon="el-icon-setting"
                                           style="font-size: 18px">
                                </el-button>
                                <el-popconfirm title="确定删除吗?" style="margin-left: 10px;"
                                               @onConfirm="handleDelete(scope.row)">
                                    <el-button type="text" icon="el-icon-delete"
                                               style="font-size: 18px"
                                               slot="reference">
                                    </el-button>
                                </el-popconfirm>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div style="text-align: right;margin-top: 8px">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                   :current-page="currentPage"
                                   :page-sizes="[10, 20, 50, 100]"
                                   :page-size="pageSize"
                                   layout="prev, pager, next, sizes,total"
                                   :total="total">
                    </el-pagination>
                </div>
            </el-card>
            <role-edit :dialogVisible="dialogVisible" :editTitle="editTitle" :rowData="rowData"
                       @callBackFromSub="callBackFromSub"></role-edit>
        </div>
        <role-permission-setting v-else :editTitle="editSettingTitle" :rowData="rowSettingData"
                                 @callBackFromSub="callBackFromSub"></role-permission-setting>
    </div>
</template>

<script>
    import RoleEdit from "./RoleEdit"
    import RolePermissionSetting from "./RolePermissionSetting"

    export default {
        name: "RoleManager",
        data() {
            return {
                tableData: [],
                currentPage: 1,
                pageSize: 10,
                total: 0,
                loading: false,
                formQuery: {
                    name: '',
                    sex: ''
                },
                rowData: {},
                rowSettingData: {},
                editTitle: "提示",
                editSettingTitle: "提示",
                dialogVisible: false,
                dialogSettingVisible: false
            }
        },
        components: {
            RoleEdit,
            RolePermissionSetting
        },
        methods: {
            handleClick(row) {
                if (row) {
                    this.dialogVisible = true;
                    this.editTitle = "修改角色";
                    this.rowData = row;
                } else {
                    this.dialogVisible = true;
                    this.editTitle = "添加角色";
                    this.rowData = {};
                }
            },
            //设置权限
            handleSettingClick(row) {
                if (row) {
                    this.dialogSettingVisible = true;
                    this.editSettingTitle = "权限管理";
                    this.rowSettingData = row;
                }
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.onSubmit(null, val);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.onSubmit(val, this.pageSize);
            },
            //查询方法
            onSubmit(currentPage, pageSize) {
                this.formQuery.currentPage = currentPage ? currentPage : 1;
                this.formQuery.pageSize = pageSize ? pageSize : this.pageSize;
                this.initTableData(this.formQuery);
            },
            onReset() {
                this.formQuery = {
                    name: ''
                };
            },
            handleDelete(row) {
                this.$httpDelete(this.$axiosBaseHttp, "api/rest/roleInfo/del/" + row.id, {}).then(res => {
                    if (res.code === 200) {
                        this.$successMsg("删除成功");
                    } else {
                        this.$errorMsg("删除失败");
                    }
                    this.onSubmit(null, null);
                });
            },
            //子组件方法回调
            callBackFromSub(param) {
                this.dialogVisible = param;
                this.dialogSettingVisible = param;
                this.initTableData(this.formQuery);
            },
            //初始化表格数据
            initTableData(param) {
                this.loading = true;
                this.$httpGet(this.$axiosBaseHttp, "api/rest/roleInfo/list/page", param).then(res => {
                    if (res.code === 200) {
                        this.total = res.data.total;
                        this.pageSize = res.data.size;
                        this.tableData = res.data.records;
                    }
                    this.loading = false;
                })
            }
        },
        mounted() {
            this.onSubmit(null, null);
        }
    }
</script>

<style scoped lang="scss">
    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }

    .clearfix:after {
        clear: both
    }

    .clearfix > span {
        display: inline-block;
        height: 30px;
        line-height: 15px;
    }

    .el-icon-condition, .el-icon-table-list {
        font-size: 20px;
        margin: 3px 3px 3px 3px;
    }

    .table-list {
        margin-top: 10px;
        height: calc(100vh - 260px);
    }

    .el-card {
        /deep/ .el-card__header {
            padding: 10px 15px;
        }
    }

    .el-table .cell {
        height: 23px;

        .el-button {
            padding: 0 0;
        }
    }
</style>